package com.andatsoft.laisim.dialog;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.activity.BaseActivity;
import com.andatsoft.laisim.activity.SoundEffectActivity;
import com.andatsoft.laisim.audioeffect.AudioEffect;
import com.andatsoft.laisim.db.DataBaseAccess;
import com.andatsoft.laisim.theme.ThemeManager;

public class PresetDialog extends BaseActivity {

	public static final String INTENT_PRESET_CHANGED = "com.laisim.preset.changed";
	public static final int RESULT_CODE_FOR_SOUND_EFFECT_ACTIVITY = 300;
	private List<AudioEffect> mPresetList;

	private ListView mListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		getWindow().setBackgroundDrawable(
				new ColorDrawable(getSelectedTheme()
						.getDialogBackgroundColor()));
		setContentView(R.layout.dialog_preset);

		// getWindow().setBackgroundDrawable(
		// new ColorDrawable(ThemeManager.getSelectedTheme()
		// .getDialogBackgroundColor()));
		loadAllPreset();
		mListView = (ListView) findViewById(R.id.list_preset_dialog);
		mListView.setAdapter(new MyAdapter(getApplicationContext()));
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				AudioEffect au = mPresetList.get(pos);
				Intent i = new Intent(PresetDialog.this,
						SoundEffectActivity.class);
				i.putExtra(INTENT_PRESET_CHANGED, au);
				setResult(RESULT_CODE_FOR_SOUND_EFFECT_ACTIVITY, i);
				PresetDialog.this.finish();
			}
		});
		((TextView) findViewById(R.id.tv_title_list_preset_dialog))
		.setTextColor(ThemeManager.getSelectedTheme()
				.getDialogTextColor());
	}

	private void loadAllPreset() {
		DataBaseAccess db = DataBaseAccess.getInstance(getApplicationContext());
		mPresetList = db.getPresets();
		if (mPresetList.size() <= 0) {
			db.importBuiltInPreset();
			mPresetList = db.getPresets();
		}
	}

	private class MyAdapter extends ArrayAdapter<AudioEffect> {
		public MyAdapter(Context context) {
			super(context, 0);
		}

		public class ViewHolder {
			public TextView tvName;
		}

		@Override
		public int getCount() {
			if (mPresetList == null)
				return 0;
			return mPresetList.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			ViewHolder vHolder;
			if (view == null) {
				view = getLayoutInflater().inflate(
						R.layout.dialog_preset_list_item, parent, false);
				vHolder = new ViewHolder();
				vHolder.tvName = (TextView) view
						.findViewById(R.id.tv_preset_list_dialog_item);
				view.setTag(vHolder);
			} else {
				vHolder = (ViewHolder) view.getTag();
			}
			// Because the view holder is holding the reference of the text
			// view...
			vHolder.tvName.setText(mPresetList.get(position).getName());
			vHolder.tvName.setTextColor(getSelectedTheme().getDialogTextColor());
			return view;
		}

	}

}
